package com.zw.a.basic.a10.demo02_list;

import com.itheima.demo01_collection.Student;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//案例:演示List集合存储自定义类型的对象并遍历.
public class Demo04 {
    public static void main(String[] args) {
        //1.定义一个学生类, 属性为姓名和年龄.
        //2.创建List集合, 用来存储学生对象.
        List<Student> list= new ArrayList<>();
        //3.往List集合中, 添加3个学生的信息.
        Student s1 = new Student("刘亦菲", 33);
        Student s2 = new Student("赵丽颖", 31);
        Student s3 = new Student("高圆圆", 35);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //4.分别通过两种遍历方式, 来遍历List集合.
        //方式一: 通过迭代器实现.
        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            /*Student stu = it.next();
            System.out.println(stu);*/

            System.out.println(it.next());
        }
        System.out.println("--------------------");

        //方式二: 通过for() + size() + get()方法实现.
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu);
        }
    }
}
